package com.mmy.controller.train;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.sys.TSysUser;
import com.mmy.domain.train.TTrainOrder;
import com.mmy.domain.train.TTrainPlan;
import com.mmy.domain.train.VTrainPlan;
import com.mmy.service.train.impl.TrainOrderServiceImpl;
import com.mmy.service.train.impl.TrainPlanServiceImpl;

@Controller
public class TrainFrontController {

	@Autowired
	TrainPlanServiceImpl trainPlanServiceImpl;
	@Autowired
	TrainOrderServiceImpl trainOrderServiceImpl;

	/**
	 * 函数功能说明： 火车票首页
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys 修改人hanxl
	 * @date 创建日期 修改时间2020.04.11
	 */
	@RequestMapping("/fronttotrainindex")
	public String fronttoTrainIndex(Model model) {

		// 国内机票推荐模块 推荐由北京出发的机票信息 前10条
		String strZoneOutName = "北京市";
		List<VTrainPlan> lstTrainPlan = trainPlanServiceImpl.findByZoneOutName(strZoneOutName);
		model.addAttribute("lstTrainPlan", lstTrainPlan);
		// 国外机票推荐模块 推荐由北京出发的机票信息 前10条
		List<VTrainPlan> lstOutTrainPlan = trainPlanServiceImpl.findOutByZoneOutName();
		model.addAttribute("lstOutTrainPlan", lstOutTrainPlan);
		return "train/TrainIndex";
	}

	/**
	 * 函数功能说明： 查询航班时刻
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间 2021.03.17
	 */
	@RequestMapping(value = "/frontsearchtrainplan")
	public String backSearchTrainPlan(HttpServletRequest request, Model model) {

		int iIsReturn = Integer.parseInt(request.getParameter("strIsReturn"));
		List<VTrainPlan> lstTrainPlan = new ArrayList<VTrainPlan>();
		if (iIsReturn == 0) {
			String strOutCityName = request.getParameter("homecity_name");
			String strcityid = request.getParameter("cityid");
			String strInCityName = request.getParameter("getcity_name");

			String strgetcityid = request.getParameter("getcityid");
			// 前台推荐机票获取的时间精确到秒
			String strOutDate = request.getParameter("strOutDate");
			lstTrainPlan = trainPlanServiceImpl.findSingle(strOutCityName, strInCityName, strOutDate);
			model.addAttribute("strOutCityName", strOutCityName);
			model.addAttribute("strcityid", strcityid);
			model.addAttribute("strInCityName", strInCityName);
			model.addAttribute("strgetcityid", strgetcityid);

			model.addAttribute("strOutDate", strOutDate);
		} else {
			String strOutCityName = request.getParameter("homecity_name2");
			String strcityid = request.getParameter("cityid");
			String strInCityName = request.getParameter("getcity_name2");
			String strgetcityid = request.getParameter("getcityid");
			String strOutDate = request.getParameter("strOutDate1");
			String strBackDate = request.getParameter("strBackDate1");
			// 去程
			lstTrainPlan = trainPlanServiceImpl.findSingle(strOutCityName, strInCityName, strOutDate);
			// 返程
			List<VTrainPlan> lstBackTrainPlan = trainPlanServiceImpl.findSingle(strInCityName, strOutCityName,
					strBackDate);
			model.addAttribute("strOutCityName2", strOutCityName);
			model.addAttribute("strcityid2", strcityid);
			model.addAttribute("strInCityName2", strInCityName);
			model.addAttribute("strgetcityid2", strgetcityid);
			model.addAttribute("strOutDate1", strOutDate);
			model.addAttribute("strBackDate1", strBackDate);
			model.addAttribute("lstBackTrainPlan", lstBackTrainPlan);

		}
		model.addAttribute("strIsReturn", iIsReturn);
		model.addAttribute("lstTrainPlan", lstTrainPlan);

		return "train/TrainSearch";

	}

	/**
	 * 函数功能说明：前台 新建飞机票预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间 2021.3.17
	 */
	@RequestMapping(value = "/enterreservetrain")
	public String EnterReserveTrain(HttpServletRequest request, Model model) {

		// 查询该景区时间在计划日期内的计划

		TTrainPlan dbTrainPlan = trainPlanServiceImpl.findByPlanID(Integer.parseInt(request.getParameter("iPlanID")));
		model.addAttribute("dbTrainPlan", dbTrainPlan);

		model.addAttribute("iPlanID", request.getParameter("iPlanID"));
		return "train/ReserveTrain";

	}

	/**
	 * 函数功能说明： 新建机票订单信息提交
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */
	@ResponseBody
	@RequestMapping(value = "/savereservetrain")
	public Map<String, Object> saveReserveTrain(@RequestParam("planID") String strPlanID,
			@RequestParam("order") String strPlaneOrder, @RequestParam("number") String strNumber,
			HttpSession session) {

		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		Map<String, Object> data = trainOrderServiceImpl.createOrderTrain(strPlanID, strNumber, strPlaneOrder,
				sessionSysUser);

		return data;
	}

	/**
	 * 函数功能说明： 新建景区电子票订单信息提交 进入填写收据页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.01.12
	 */
	@RequestMapping(value = "/topayordertrain")
	public String toPayOrderTrain(HttpServletRequest request, Model model) {
		String lstID = request.getParameter("lstID");
		// 拆分字符串
		String[] arrID = lstID.split(",");
		TTrainOrder dbTrainOrder = trainOrderServiceImpl.findByID(Integer.parseInt(arrID[0]));
		model.addAttribute("iOrderAmount", dbTrainOrder.getOrderAmount());
		model.addAttribute("lstID", lstID);
		return "train/PayOrderTrain";
	}

	/**
	 * 函数功能说明：收据提交
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 2021.01.12
	 */
	@RequestMapping(value = "/savepayordertrain")
	public String savePayOrderTrain(TTrainOrder tTrainOrder, HttpSession session, String lstID, Model model) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		List<TTrainOrder> lstTrainOrder = trainOrderServiceImpl.updateTrainOrder(tTrainOrder, sessionSysUser, lstID);

		model.addAttribute("lstID", lstID);
		model.addAttribute("tTrainOrder", lstTrainOrder.get(0));
		model.addAttribute("lstTrainOrder", lstTrainOrder);
		return "train/PrintTrainOrder";

	}

	/**
	 * 函数功能说明： 火车票搜索
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期
	 */
	@RequestMapping("/trainsearch")
	public String searchTrain(HttpServletRequest request, Model model) {

		/*
		 * String start = request.getParameter("start"); String end =
		 * request.getParameter("end"); String date = request.getParameter("date");
		 */
		List<VTrainPlan> lstTrainPlan = trainPlanServiceImpl.findAll();
		model.addAttribute("trainBase", lstTrainPlan);
		return "train/trainindex";
	}

	/**
	 * 函数功能说明： 跳转--火车票预留页
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping("/reservetrain")
	public String EnterReserveTrainBase(HttpServletRequest request, Model model) {

		int iPlanID = Integer.parseInt(request.getParameter("iPlanID"));
		TTrainPlan trainPlan = trainPlanServiceImpl.findByPlanID(iPlanID);
		model.addAttribute("plan", trainPlan);
		return "webbackground/train/reservetrainbase";

	}
}
